package com.hc.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hc.common.service.RedisService;
import com.hc.constant.RedisConst;
import com.hc.domain.Province;
import com.hc.mapper.ProvinceMapper;
import com.hc.service.ProvinceService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hecai
 */
@Service
@CacheConfig(cacheManager = "cacheManager")
public class ProvinceServiceImpl extends ServiceImpl<ProvinceMapper, Province> implements ProvinceService {

    @Resource
    private RedisService<List<Province>> provinceListRedisServiceImpl;

    @Override
    @Cacheable(value = RedisConst.PROVINCE, key = "#key", condition = "#flag==true")
    public List<Province> findAllProvince(String key, Boolean flag) {
        List<Province> provinceList = baseMapper.selectList(null);
        ////排序（如果初始化化、增加、修改正确，下面代码可以省略）
        //provinceList.sort((item1, item2) -> {
        //    int t1 = item1.getPriority() == null ? 0 : item1.getPriority();
        //    int t2 = item2.getPriority() == null ? 0 : item2.getPriority();
        //    return t2 - t1;
        //});
        return provinceList;
    }

    @Override
    @CachePut(value = RedisConst.PROVINCE, key = "#province.id", condition = "#flag==true")
    public Province insertProvince(Province province, Boolean flag) {
        int insertRes = baseMapper.insert(province);
        if (insertRes == 1) {
            //更新Province列表
            List<Province> provinceList = provinceListRedisServiceImpl.get(RedisConst.PROVINCE + "::all");
            provinceList.add(province);
            //排序
            provinceList.sort((item1, item2) -> {
                int t1 = item1.getPriority() == null ? 0 : item1.getPriority();
                int t2 = item2.getPriority() == null ? 0 : item2.getPriority();
                return t2 - t1;
            });
            provinceListRedisServiceImpl.set(RedisConst.PROVINCE + "::all", provinceList);
        }
        return province;
    }

    @Override
    @Cacheable(value = RedisConst.PROVINCE, key = "#id", condition = "#flag==true")
    public Province findProvinceById(Integer id, Boolean flag) {
        Province province = baseMapper.selectById(id);
        //如果正常初始化，正常增加、修改，下面代码可以省略
        //if (province != null) {
        //    //如果Redis中没有则从数据库中查，手工将数据库中查寻到的数据缓存到Redis的Province列表中
        //    List<Province> provinceList = provinceListRedisUtil.get(RedisConst.PROVINCE + "::all");
        //    if (!provinceList.contains(province)) {
        //        provinceList.add(province);
        //        ////排序(如果缓存中的数据已经有序的话，此处就不需要排序了)
        //        //provinceList.sort((item1, item2) -> {
        //        //    int t1 = item1.getPriority() == null ? 0 : item1.getPriority();
        //        //    int t2 = item2.getPriority() == null ? 0 : item2.getPriority();
        //        //    return t2 - t1;
        //        //});
        //        provinceListRedisUtil.set(RedisConst.PROVINCE + "::all", provinceList);
        //    }
        //}
        return province;
    }

    @Override
    @CachePut(value = RedisConst.PROVINCE, key = "#province.id", condition = "#flag==true")
    public Province updateProvince(Province province, Boolean flag) {
        int updateRes = baseMapper.updateById(province);
        if (updateRes == 1) {
            province = baseMapper.selectById(province.getId());
            //目标城市下的所有Province
            List<Province> provinceList = provinceListRedisServiceImpl.get(RedisConst.PROVINCE + "::all");
            for (int i = 0; i < provinceList.size(); i++) {
                Province item = provinceList.get(i);
                //如果参数country在目标城市中，即没有换城市
                if (item.getId().equals(province.getId())) {
                    provinceList.set(i, province);
                    break;
                }
            }
            provinceListRedisServiceImpl.set(RedisConst.PROVINCE + "::all", provinceList);
        }
        return province;
    }

    @Override
    @CacheEvict(value = RedisConst.PROVINCE, key = "#id", condition = "#flag==true")
    public Province deleteProvinceById(Integer id, Boolean flag) {
        int deleteRes = baseMapper.deleteById(id);
        if (deleteRes == 1) {
            //目标城市下的所有Province
            List<Province> provinceList = provinceListRedisServiceImpl.get(RedisConst.PROVINCE + "::all");
            for (int i = 0; i < provinceList.size(); i++) {
                Province item = provinceList.get(i);
                //如果参数country在目标城市中，即没有换城市
                if (item.getId().equals(id)) {
                    provinceList.remove(i);
                    break;
                }
            }
            provinceListRedisServiceImpl.set(RedisConst.PROVINCE + "::all", provinceList);
        }
        return new Province();
    }

}
